package com.zxl.yjb.activity.home.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.MyFragmentCommentAdapter;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.CommenBean;
import com.zxl.yjb.bean.CommentBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;
import com.zxl.yjb.widget.CircleImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserSpaceActivity extends BaseActivity implements OnRefreshLoadMoreListener,View.OnClickListener {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.iv_down)
    ImageView iv_down;
    @BindView(R.id.ll_down_or_up)
    LinearLayout ll_down_or_up;
    @BindView(R.id.iv_confirm)
    ImageView iv_confirm;
    @BindView(R.id.iv_confirm2)
    ImageView iv_confirm2;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    @BindView(R.id.rv_comment_list)
    RecyclerView rv_comment_list;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refresh_layout;

    private String uid = "";

    private MyFragmentCommentAdapter adapter;
    private int currentPage = 1;
    private int pageSize = 20;

    private View headerVeiw;
    private CircleImageView ivMyHeader;
    private TextView tvMyName;
    private TextView tvComment;
    private TextView tvScore;
    private TextView tv_focus;
    private LinearLayout llMyInfo;

    private DataManager dataManager;
    private String relation = "";
    private LoginSucessBean userinfo;
    private LoginSucessBean loginSucessBean;
    private TextView tvMyUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userspace);
        ButterKnife.bind(this);
        uid = getIntent().getStringExtra("uid");
        loginSucessBean = (LoginSucessBean) SaveBeanUtil.getBean(this, Const.VIPINFO);
        initView();
        getCommenList(false);
        refreshUserInfo();
    }

    @Override
    protected void initView() {



        //评论列表以上部分
        headerVeiw = LayoutInflater.from(this).inflate(R.layout.header_user_space, null);
        llMyInfo = headerVeiw.findViewById(R.id.ll_my_info);
        ivMyHeader = headerVeiw.findViewById(R.id.iv_my_header);
        tvMyName = headerVeiw.findViewById(R.id.tv_my_name);
        tvComment = headerVeiw.findViewById(R.id.tv_comment);
        tvScore = headerVeiw.findViewById(R.id.tv_score);
        tv_focus = headerVeiw.findViewById(R.id.tv_focus);
        tvMyUid = headerVeiw.findViewById(R.id.tv_my_uid);

        llMyInfo.setOnClickListener(this);
        tv_focus.setOnClickListener(this);

        if (uid.equals(loginSucessBean.getUid()+""))
        {
            tv_page_title.setText("我的空间");
            tv_focus.setVisibility(View.GONE);
        }
        else
        {
            tv_page_title.setText("他的空间");
            tv_focus.setVisibility(View.VISIBLE);
        }

        initAdapter();
    }



    private void initAdapter() {
        //创建布局管理
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_comment_list.setLayoutManager(layoutManager);
        //创建适配器
        adapter = new MyFragmentCommentAdapter(R.layout.item_comment);
        adapter.addHeaderView(headerVeiw);
        //给RecyclerView设置适配器
        rv_comment_list.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                ToastUtils.show(UserSpaceActivity.this,position+"");
            }
        });
        refresh_layout.setOnRefreshLoadMoreListener(this);

    }


    private void setUserinfo(LoginSucessBean loginSucessBean)
    {
        Glide.with(this).load(loginSucessBean.getFace_img()).into(ivMyHeader);
        tvMyName.setText(loginSucessBean.getNickname());
        tvMyUid.setText("uid:"+loginSucessBean.getUid());
        relation = loginSucessBean.getAttribute_2me_0or1or2();
        switch (relation) {
            case "0"://没关系
                tv_focus.setText("关注");
                break;

            case "1"://好友关系
                tv_focus.setText("取消关注");
                tv_focus.setBackgroundColor(getResources().getColor(R.color.cp_color_gray_light));
                break;

            case "2"://黑名单关系
                tv_focus.setVisibility(View.GONE);
                break;

            default:
                break;
        }
    }


    //关注
    private void addFriends() {
        DataManager dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("relationid",uid);
        map.put("attribute","1");
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object callback) {
                tv_focus.setText("取消关注");
                tv_focus.setBackgroundColor(getResources().getColor(R.color.cp_color_gray));
            }
        };
        dataManager.addFriend(new ProgressObserver<Object>(listener,this),map);
    }


    //关注
    private void delFriends() {
        DataManager dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object callback) {
                tv_focus.setText("关注");
                tv_focus.setBackgroundColor(getResources().getColor(R.color.main_color));
            }
        };
        dataManager.deletefriend(new ProgressObserver<Object>(listener,this),uid);
    }




    /**
     * 获取个人信息
     */
    private void refreshUserInfo() {
        DataManager dataManager = new DataManager();
        dataManager.getUserInfo(new ProgressObserver<LoginSucessBean>(new ObserverOnNextListener<LoginSucessBean>() {
            @Override
            public void onNext(LoginSucessBean loginSucessBean) {
                if (loginSucessBean == null)
                    return;
                userinfo = loginSucessBean;
                setUserinfo(loginSucessBean);

            }
        }, this),uid);
    }


    /**
     * 获取评论列表
     * @param isRefresh
     */
    private void getCommenList(final boolean isRefresh) {
        DataManager dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("uid",uid+"");
        map.put("page",currentPage+"");
        map.put("count",pageSize+"");
        ObserverOnNextListener<CommentBean> listener = new ObserverOnNextListener<CommentBean>() {
            @Override
            public void onNext(CommentBean commenBean) {
                tvComment.setText(commenBean.getTotal_count());
                tvScore.setText(commenBean.getEval_avg());
                currentPage++;
                if (isRefresh) {
                    adapter.setNewData(commenBean.getEval_arr());
                    refresh_layout.finishRefresh();
                }else {
                    adapter.addData(commenBean.getEval_arr());
                    refresh_layout.finishLoadMore();
                    if (commenBean.getEval_arr().size()<pageSize){
                        refresh_layout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }
            }
        };
        dataManager.evaluate_other2uid_list(new ProgressObserver<CommentBean>(listener,this){
            @Override
            public void showDialog() {
            }
        },map);

    }



    @OnClick({R.id.iv_back, R.id.tv_page_title, R.id.rv_comment_list, R.id.refresh_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_page_title:
                break;
            case R.id.rv_comment_list:
                break;
            case R.id.refresh_layout:
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore(5000);
        getCommenList(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh(5000);
        currentPage=1;
        getCommenList(true);
        refreshLayout.setNoMoreData(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.ll_my_info:
                if (userinfo == null)
                    return;
                Intent intent = new Intent(UserSpaceActivity.this,UserInfoActivity.class);
                intent.putExtra("uid",userinfo.getUid());
                intent.putExtra("nickname",userinfo.getNickname());
                intent.putExtra("headimage",userinfo.getFace_img());
                intent.putExtra("sex",userinfo.getSex()+"");
                intent.putExtra("birthday",userinfo.getBirthday());
                startActivity(intent);
                break;

            case R.id.tv_focus:
                if ("关注".equals(tv_focus.getText().toString()))
                {
                    addFriends();
                }
                else if ("取消关注".equals(tv_focus.getText().toString()))
                {
                    delFriends();
                }
                break;

            default:
                break;
        }
    }
}
